<?php

namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Models\Common\CollOption;
use App\Models\Common\CollTwoOption;
use Validator;

class SignServiceProvider extends ServiceProvider{
    public function boot(){
        //自定义标识验证匹配改写
        Validator::extend('sign_match', function($attribute, $value, $parameters) {
            $result = CollOption::where('sign', $value)->where('opt_id', $parameters[0])->count();
            if($result){
                return false;
            }else{
                return true;
            }
        });


        //更新标识和编码唯一
        Validator::extend('update_sign_match', function($attribute, $value, $parameters) {
            $result = CollOption::where('sign', $value)->where('opt_id', $parameters[0])->where('id','!=', $parameters[1])->count();
            if($result){
                return false;
            }else{
                return true;
            }
        });

        //二级分类选项自定义标识验证匹配改写
        Validator::extend('two_sign_match', function($attribute, $value, $parameters) {
            $result = CollTwoOption::where('cid', $value)->where('pid', $parameters[0])->count();
            if($result){
                return false;
            }else{
                return true;
            }
        });

        //二级分类选项自定义标识验证匹配改写
        Validator::extend('update_two_sign_match', function($attribute, $value, $parameters) {
            $result = CollTwoOption::where('cid', $value)->where('pid', $parameters[0])->where('id','!=', $parameters[1])->count();
            if($result){
                return false;
            }else{
                return true;
            }
        });

        //自定义类别编号匹配改写
        Validator::extend('cid_match', function($attribute, $value, $parameters) {
            $one = '0000';
            $two = substr($parameters[1],0,2);
            $three = substr($parameters[1],0,4);
            $end = '00';
            if($parameters[0] == '1'){
                if(preg_match_all("/\d{2}".$one."$/Uis", $value, $matches)){
                    return true;
                }else{
                    return false;
                }
            }else if($parameters[0] == '2'){
                if(preg_match_all("/^".$two."\d{2}".$end."$/Uis", $value, $matches)){
                    return true;
                }else{
                    return false;
                }
            }else if($parameters[0] == '3'){
                if(preg_match_all("/^".$three."\d{2}/Uis", $value, $matches)){
                    return true;
                }else{
                    return false;
                }
            }
        });

        //自定义类别编号匹配改写
        Validator::extend('cate_match', function($attribute, $value, $parameters) {
            $one = '0000';
            if(preg_match_all("/\d{2}".$one."$/Uis", $value, $matches)){
                return true;
            }else{
                return false;
            }
        });

     //自定义URL匹配改写
        Validator::extend('check_url', function($attribute, $value, $parameters) {
            $url_type = $parameters[0];
            if($url_type == '1'){
                $preg="/^(([0-9a-zA-Z]+)(:\/\/))?(([0-9a-zA-Z]+)\.)?(\w+)\.([\w.]+)(\/([\w]+)\/?)*([\w-\.\/\-\?\&\%\=]*)?/";
                if(preg_match($preg, $value, $matches)){
                    return true;
                }else{
                    return false;
                }
            }else{
                if(preg_match("/^[1-9]{1}(\d)*$/", $value, $matches)){
                    return true;
                }else{
                    return false;
                }
            }
        });

        //自定义URL匹配改写
        Validator::extend('check_link', function($attribute, $value, $parameters) {
            $preg="/^(([0-9a-zA-Z]+)(:\/\/))?(([0-9a-zA-Z]+)\.)?(\w+)\.([\w.]+)(\/([\w]+)\/?)*([\w-\.\/\-\?\&\%\=]*)?/";
            if(preg_match($preg, $value, $matches)){
                return true;
            }else{
                return false;
            }
        });

    }

    /**
     * Register the application services.
     *
     * @return void
     */

    public function register(){

    }
}
